package com.three.service.impl;

import com.baor.baseUtil.R;
import com.three.dao.PrestudentsDao;
import com.three.dao.StudentDao;
import com.three.dao.UserDao;
import com.three.domain.Students;
import com.three.domain.Users;
import com.three.service.PrestudentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
     public class PrestudentsServiceImpl implements PrestudentsService {
        @Autowired
        private PrestudentsDao preStudentsDao;
        @Autowired
        private UserDao userDao;
        @Autowired
        private StudentDao studentDao;

        @Override
        public void updateStudentType() {
        preStudentsDao.updateStudentType(); // 调用 DAO 方法更新学生类型
        }

        @Override
        public R prestudentsList(Integer pageIndex, Integer pageSize, Integer xiaoshou_id) {
//          updateStudentType(); // 在查询之前更新学生类型
            pageIndex = (pageIndex - 1) * pageSize;
            double pageCountprestudents =preStudentsDao.PageCountpreStudents(xiaoshou_id);

            pageCountprestudents = Math.ceil(pageCountprestudents / pageSize);
            List<Students> studentsList = preStudentsDao.prestudentsList(pageIndex, pageSize,xiaoshou_id);
            Map<String, Object> map = new HashMap<>();
            map.put("pageCountprestudents", pageCountprestudents);
            map.put("studentsList", studentsList);
            return R.ok(map);
        }

        @Override
        public R addpre(Students students) {
            LocalDate enrollment_date = LocalDate.parse(students.getEnrollment_date());
            LocalDate today = LocalDate.now();
            String phone = students.getPhone();
            if (enrollment_date.isBefore(today) || enrollment_date.isEqual(today)) {
                Users user = new Users();
                user.setUsername(students.getUsername());
                user.setGender(students.getGender());
                user.setRole(5);
                user.setPhone(students.getPhone());
                user.setUsername(students.getPhone());
                userDao.addUser(user);
                studentDao.updateUser_id(phone);
            }
            String major = students.getMajor();
            String class_name = null;
            String graduation_date = null;
            if (major != null) {
                class_name = preStudentsDao.getClassName(major, String.valueOf(enrollment_date));
                graduation_date = preStudentsDao.getGraduationDate(class_name);

            }
            students.setClass_name(class_name);
            students.setGraduation_date(graduation_date);
            int row = this.preStudentsDao.addpre(students);
            if (row > 0) return R.ok();
            else return R.er();
        }

    //    修改更新预招生信息
        @Override
        public R updatepreStudent(Students student) {
            LocalDate enrollment_date = LocalDate.parse(student.getEnrollment_date());
            LocalDate today = LocalDate.now();
            String phone = student.getPhone();
            String major = student.getMajor();
            String class_name = null;
            String graduation_date = null;
            if (major != null) {
                class_name = preStudentsDao.getClassName(major, String.valueOf(enrollment_date));
                System.out.println(class_name);
                graduation_date = preStudentsDao.getGraduationDate(class_name);

            }
            student.setClass_name(class_name);
            student.setGraduation_date(graduation_date);
            if (enrollment_date.isBefore(today) || enrollment_date.isEqual(today)) {
                Users user = new Users();
                user.setName(student.getName());
                user.setGender(student.getGender());
                user.setRole(5);
                user.setPhone(student.getPhone());
                user.setUsername(student.getPhone());
                user.setPassword("123456");
                userDao.addUser(user);
                studentDao.updateUser_id(phone);
            }
            return R.ok(preStudentsDao.updatepreStudent(student));
        }
        //软删除预招生
        @Override
        public void softDeletepreStudent(int student_id) {
            preStudentsDao.softDeletepreStudent(student_id);
        }

        @Override
        public R findpreStudentsByName(Integer pageIndex, Integer pageSize,String name, Integer xiaoshou_id) {
            pageIndex = (pageIndex - 1) * pageSize;
            double pageCountprekey =preStudentsDao.PageCountprekey(pageIndex, pageSize,name,xiaoshou_id);
            System.out.println(pageCountprekey);
            pageCountprekey = Math.ceil(pageCountprekey / pageSize);
            List<Students> prekeyList = preStudentsDao.getPreByName(pageIndex, pageSize,name,xiaoshou_id);
            Map<String, Object> map = new HashMap<>();
            map.put("pageCountprekey", pageCountprekey);
            map.put("prekeyList", prekeyList);
            return R.ok(map);

        }

    @Override
    public R updateStatus() {
        preStudentsDao.updateStatus();
        return R.ok(preStudentsDao.ccgc());
    }
//          return preStudentsDao.getStudentsByName(name);
//        }

}
